<template>
<div class="scheduleManage">
  <y-table
    ref="table"
    border
    pagination
    :getDataWay="getDataWay"
    :columns="columns">
    <el-button type="primary" @click="addSchedule" slot="button">添加定时任务</el-button>
    <el-table-column slot="status" label="启用" align="center" header-align="center" width="80">
      <template v-slot="{row}">
        <el-popconfirm
          :title="row.status === '1' ? `是否确认停用？` : `是否确认启用？`"
          @confirm="doEnable(row, row.status === '0')"
        >
          <el-switch
            disabled
            slot="reference"
            v-model="row.status"
            active-value="1"
            inactive-value="0"></el-switch>
        </el-popconfirm>
      </template>
    </el-table-column>
    <el-table-column slot="y-oper" label="操作" align="center" header-align="center" width="80">
      <template v-slot="{row}">
        <div class="table-buttons">
          <el-button type="primary" size="mini" @click="editSchedule(row)">编辑</el-button>
        </div>
      </template>
    </el-table-column>
  </y-table>
  <y-form
    v-model="forms.formModel"
    :visible.sync="forms.showForm"
    :title="forms.formTypeTitle[forms.formsType]"
    destroy-on-close
    width="50%"
    :form-attrs="forms.formAttrs"
    :columns="forms.formColumns"
    @ensure="ensureModel">
  </y-form>
</div>
</template>

<script>
export default {
  name: "scheduleManage",
  computed:{
    getDataWay(){
      return{
        dataType: 'url',
        data: this.$api.system.schedulesPage,
        param: {}
      }
    }
  },
  data(){
    return {
      columns:[
        {
          label: "任务名",
          prop: "name"
        },
        {
          label: "任务标识(Bean名称)",
          prop: "taskKey"
        },
        {
          label: "表达式",
          prop: "cron"
        },
        {
          prop: "status",
          template: true
        }
      ],
      forms:{
        formsType: '',
        formTypeTitle: {
          'ADD': '添加定时任务',
          'EDIT': '编辑定时任务'
        },
        formModel:{
          name: '',
          taskKey: '',
          cron: ''
        },
        currentForm: null,
        showForm: false,
        formAttrs:{
          labelWidth: '100px',
          rules: {
            name: [{required: true, message: "请输入任务名称", trigger: "blur"}],
            taskKey: [{required: true, message: "请输入任务标识", trigger: "blur"}],
            cron: [{required: true, message: "请输入表达式", trigger: "blur"}]
          }
        },
        formColumns:[
          {
            label: "任务名称",
            prop: "name",
            comp:{
              compType: 'input',
              attrs:{
                maxlength: 20,
                showWordLimit: true
              }
            }
          },
          {
            label: "任务标识",
            prop: "taskKey",
            comp:{
              compType: 'input',
              attrs:{
                maxlength: 100,
                showWordLimit: true
              }
            }
          },
          {
            label: "表达式",
            prop: "cron",
            comp:{
              compType: 'input',
              attrs:{
                maxlength: 20,
                showWordLimit: true
              }
            }
          }
        ],
      }
    }
  },
  methods:{
    resetForm() {
      Object.keys(this.forms.formModel).map(key => {
        this.forms.formModel[key] = this.forms.currentForm ? this.forms.currentForm[key] : '';
      })
    },
    search(jumpToFirst){
      this.$refs['table'].loadData({}, jumpToFirst);
    },
    addSchedule(){
      this.forms.formsType = 'ADD';
      this.forms.currentForm = null;
      this.resetForm();
      this.forms.showForm = true;
    },
    editSchedule(row){
      this.forms.formsType = 'EDIT';
      this.forms.currentForm = row;
      this.resetForm();
      this.forms.showForm = true;
    },
    doEnable(row, bool){
      let url = bool ? this.$api.system.openSchedule : this.$api.system.closeSchedule;
      this.$yAxios({
        url,
        data:{
          id: row.id
        },
        loading: true
      }, info => {
        this.$message({
          type: 'success',
          message: bool ? '启用成功' : '停用成功'
        });
        this.search();
      })
    },
    ensureModel(form){
      let newForm;
      if(this.forms.formsType === 'EDIT'){
        newForm = Object.assign({}, this.forms.currentForm, form);
      }else{
        newForm = Object.assign({ status: '0' }, form);
      }
      this.$yAxios({
        url: this.$api.system.saveSchedule,
        data: newForm,
        loading: true
      }, info => {
        this.$message({
          type: 'success',
          message: '保存成功'
        });
        this.search(this.forms.formsType === 'ADD');
      })
    }
  }
}
</script>

<style scoped>
.scheduleManage{
  margin-top: -20px;
}
</style>